package com.ruoyi.bizsys.service.impl;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
    import com.ruoyi.bizsys.mapper.CreditApplyMapper;
import com.ruoyi.bizsys.domain.CreditApply;
import com.ruoyi.bizsys.service.ICreditApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.CollectionUtils;


/**
 * 授信申请Service业务层处理
 *
 * @author african-loan
 * @date 2021-12-30
 */
@Service
public class CreditApplyServiceImpl extends ServiceImpl<CreditApplyMapper,CreditApply> implements ICreditApplyService {

    @Autowired
    private CreditApplyMapper creditApplyMapper;

    /**
     * 查询授信申请
     *
     * @param id 授信申请ID
     * @return 授信申请
     */
    @Override
    public CreditApply selectCreditApplyById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询授信申请列表
     *
     * @param creditApply 授信申请
     * @return 授信申请
     */
    @Override
    public List<CreditApply> selectCreditApplyList(CreditApply creditApply) {
        return this.list(new QueryWrapper<>(creditApply).last("order by id desc"));
    }

    /**
     * 新增授信申请
     *
     * @param creditApply 授信申请
     * @return 结果
     */
            @Override
    public int insertCreditApply(CreditApply creditApply) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            creditApply.setCreateTime(DateUtils.getNowDate());
                                                                                                            return baseMapper.insert(creditApply);
    }

    /**
     * 修改授信申请
     *
     * @param creditApply 授信申请
     * @return 结果
     */
            @Override
    public int updateCreditApply(CreditApply creditApply) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    creditApply.setUpdateTime(DateUtils.getNowDate());
                                                                    return baseMapper.updateById(creditApply);
    }

    /**
     * 批量删除授信申请
     *
     * @param ids 需要删除的授信申请ID
     * @return 结果
     */
            @Override
    public int deleteCreditApplyByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除授信申请信息
     *
     * @param id 授信申请ID
     * @return 结果
     */
    @Override
    public int deleteCreditApplyById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public List<CreditApply> getReturnRunCreditData(Integer andonTaskMinute) {
        return baseMapper.getReturnRunCreditData(andonTaskMinute);
    }



    @Override
    public CreditApply getOneByCust(String custNo) {
        QueryWrapper<CreditApply> creditApplyQueryWrapper = new QueryWrapper<CreditApply>().eq("cust_no", custNo).last("order by id desc limit 1");
        return this.getOne(creditApplyQueryWrapper);
    }


    @Override
    public CreditApply getOneByUserId(String userId) {
        QueryWrapper<CreditApply> creditApplyQueryWrapper = new QueryWrapper<CreditApply>().eq("user_id", userId).last("order by id desc limit 1");
        return this.getOne(creditApplyQueryWrapper);
    }

}
